<template>
    <div>
        <div class="operarea">
            <!-- <div class="seach">
                <el-input v-model="seachForm.name" :placeholder="$t('rolename')" clearable size="mini"></el-input>
                <el-button size="mini" icon="el-icon-search" @click="seachData"></el-button>
            </div> -->
            <div class="btnList">
                <el-button size="mini" @click="addDialogVisible = true">{{$t('add')}}</el-button>
            </div>
        </div>
        <el-card class="cardBox">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>首页</el-breadcrumb-item>
                <el-breadcrumb-item>角色管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-table style="width: 100%" stripe :data="roleDataTab" v-adaptive="{bottomOffset: 85}" height="100">
                <el-table-column prop="name" :label="$t('rolename')">
                </el-table-column>
                <el-table-column prop="status" :label="$t('status')">
                    <template v-slot="scope">
                        <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="0" :inactive-value="1" @change="handleStatus(scope.row)"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column prop="address" :label="$t('authorization')">
                    <template v-slot="scope">
                        <el-button @click="showPower(scope.row.id)" size="mini">{{$t('select')}}</el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="address" :label="$t('operation')" width="150">
                    <template v-slot="scope">
                        <el-button type="primary" @click="showModify(scope.row.id)" size="mini">编辑</el-button>
                        <el-button type="primary" @click="showDel(scope.row.id)" size="mini">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                small
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :pager-count="11"
                :current-page.sync="seachForm.pageNo"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="seachForm.pageSize"
                layout="sizes, prev, pager, next"
                :total="total">
            </el-pagination>
        </el-card>
        <!-- 添加 -->
        <el-dialog :title="$t('add')" :visible.sync="addDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="addDlogClose(addDataForm)">
            <el-form :model="addDataForm" :rules="addRules" :ref="addDataForm" class="motuoForm">
                <el-form-item :label="$t('rolename')" label-width="70px" prop="name">
                    <el-input v-model="addDataForm.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('description')" label-width="70px" prop="description">
                    <el-input v-model="addDataForm.description" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('status')" label-width="70px" prop="status">
                    <el-radio-group v-model="addDataForm.status">
                        <el-radio :label="1">{{$t('forbidden')}}</el-radio>
                        <el-radio :label="0">{{$t('invoke')}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="addData(addDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 修改 -->
        <el-dialog :title="$t('modify')" :visible.sync="modifyDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="modifyDlogClose(modifyDataForm)">
            <el-form :model="modifyDataForm" :rules="modifyRules" :ref="modifyDataForm" class="motuoForm">
                <el-form-item :label="$t('rolename')" label-width="70px" prop="name">
                    <el-input v-model="modifyDataForm.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('description')" label-width="70px" prop="description">
                    <el-input v-model="modifyDataForm.description" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <!-- <el-form-item :label="$t('status')" label-width="70px" prop="status">
                    <el-radio-group v-model="modifyDataForm.status">
                        <el-radio :label="0">{{$t('forbidden')}}</el-radio>
                        <el-radio :label="1">{{$t('invoke')}}</el-radio>
                    </el-radio-group>
                </el-form-item> -->
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="modifyDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="modifyData(modifyDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 删除 -->
        <el-dialog :title="$t('hint')" :visible.sync="delDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>{{$t('confirmdel')}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="delData">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 权限 -->
        <el-dialog :title="$t('authorization')" class="smallDlog" :visible.sync="powerDialogVisible" width="30%" @close="powerDialogVisibleClosed" :modal-append-to-body="false">
            <el-tree :data="powerForm" show-checkbox :ref="powerForm" node-key="id" :default-checked-keys="selForm" default-expand-all></el-tree>
            <span slot="footer" class="dialog-footer">
                <el-button @click="powerDialogVisible = false" v-preventReClick>{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="editPower(powerForm)" v-preventReClick>{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    getSysRoleList,
    addSysRole,
    removeSysRole,
    modifySysRoleStatus,
    getSysRoleDetails,
    getRolesMenu,
    updateRolesMenu
} from '@/libs/reqlist.js'
export default {
    data () {
        return {
            total: 0,
            seachForm: {
                pageNo: 1,
                pageSize: 10
            },
            roleDataTab: [],
            addDialogVisible: false,
            addDataForm: {
                name: '',
                description: '',
                status: 0
            },
            addRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                status: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ]
            },
            powerDialogVisible: false,
            powerForm: [],
            // 勾选菜单数组
            defKeys: [],
            selForm: [],
            modifyDialogVisible: false,
            modifyDataForm: {
                id: '',
                name: '',
                description: '',
                status: 0
            },
            modifyRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ]
            },
            delDialogVisible: false,
            delRoleId: '',
            modifyStatusForm: {
                id: '',
                status: 0
            },
            updateRoleMenu: {
                roleId: 0,
                menuIds: []
            }
        }
    },
    methods: {
        getData () {},
        seachData () {
            getSysRoleList(this.seachForm).then(res => {
                if (res.data.code === 0) {
                    this.total = res.data.data.hits
                    this.roleDataTab = res.data.data.data
                }
            })
        },
        handleSizeChange (newSize) {
            this.seachForm.pageSize = newSize
            this.seachData()
        },
        handleCurrentChange (newNum) {
            this.seachForm.pageNo = newNum
            this.seachData()
        },
        // 添加
        addDlogClose (form) {
            this.$refs[form].resetFields()
        },
        addData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                addSysRole(JSON.stringify(this.addDataForm)).then(res => {
                    if (res.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('sucadd')
                        })
                        this.addDialogVisible = false
                        this.seachData()
                    }
                })
                
            })
        },
        // 权限
        showPower (id) {
            getRolesMenu(JSON.stringify({ id: id })).then(res => {
                if (res.data.code == 0) {
                    res.data.data.forEach(item => {
                        item.label = item.text
                        item.children.forEach(item1 => {
                            item1.label = item1.text
                            item1.children.forEach(item2 => {
                                item2.label = item2.text
                            })
                        })
                        this.getLeafKeys(item, this.defKeys)
                    })
                    this.powerForm = res.data.data
                    this.defKeys.forEach(item => {
                        switch (item.hasAuthority) {
                            case true:
                                this.selForm.push(item.id);
                                break;
                            case false:
                                break;
                        }
                    })
                    this.updateRoleMenu.roleId = id
                    this.powerDialogVisible = true
                }
            })
        },
        powerDialogVisibleClosed () {
            this.defKeys = []
            this.selForm = []
        },
        editPower (form) {
            let keys = [
                ...this.$refs[form].getCheckedKeys(),
                ...this.$refs[form].getHalfCheckedKeys()
            ]
            updateRolesMenu({ roleId: this.updateRoleMenu.roleId, menuIds: keys }).then(res => {
                if (res.data.code == 0) {
                    this.powerDialogVisible = false
                    this.$message({
                        type: 'success',
                        message: this.$t('modifysuc')
                    })
                }
            })
        },
        showModify (id) {
            getSysRoleDetails(id).then(res => {
                if (res.data.code === 0) {
                    this.modifyDataForm = res.data.data
                    delete this.modifyDataForm.createTime
                    delete this.modifyDataForm.updateTime
                }
            })
            this.modifyDialogVisible = true
        },
        modifyDlogClose (form) {
            this.$refs[form].resetFields()
        },
        modifyData (form) {
            this.$refs[form].validate((valid) => {
                if (!valid) return
                addSysRole(JSON.stringify(this.modifyDataForm)).then(res => {
                    if (res.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('modifysuc')
                        })
                        this.modifyDialogVisible = false
                        this.seachData()
                    }
                })
            })
        },
        showDel (id) {
            this.delRoleId = id
            this.delDialogVisible = true
        },
        delData () {
            removeSysRole(this.delRoleId).then(res => {
                if (res.data.code === 0) {
                    this.$message({
                        type: 'success',
                        message: this.$t('delsuc')
                    })
                    this.delDialogVisible = false
                    this.seachData()
                }
            })
            
        },
        handleStatus (item) {
            this.modifyStatusForm.id = item.id
            switch (item.status) {
                case 0:
                    item.status = 1
                    break
                case 1:
                    item.status = 0
                    break
            }
            this.$confirm(this.$t('whethertomodifytherolestate'), this.$t('hint'), {
                confirmButtonText: this.$t('confirm'),
                cancelButtonText: this.$t('cancel'),
                cutomClass: 'messageBox-lion'
            }).then(() => {
                this.modifyStatusForm.status = item.status
                modifySysRoleStatus(this.modifyStatusForm).then(res => {
                    if (res.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('statuschangesuccessful')
                        })
                        this.seachData()
                    }
                })
            }).catch(() => {})
        },
        // 默认菜单勾选
        getLeafKeys(node, arr) {
            switch (node.hasChildren) {
                // 如果当前节点不包括children属性,不再向下遍历
                case false:
                    arr.push({ id: node.id, hasAuthority: node.hasAuthority })
                    break;
                case true:
                    // 到这里就表示不是最下层路由,继续循环下面的children
                    node.children.forEach(item => this.getLeafKeys(item, arr))
                    break;
            }
            
        }
    },
    mounted () {
        this.seachData()
    }
}
</script>

<style lang="less" scoped>
@import './role.less';
</style>
